package com.jsgt.greekfire.ui.mine.coindetail.recharge.rechargerecord

import android.content.Context
import android.content.Intent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.PAGE_SIZE
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.BillBean
import com.jsgt.greekfire.bean.GFTKLineData
import com.jsgt.greekfire.factory.UserViewModelFactory
import com.jsgt.greekfire.ui.mine.coindetail.recharge.rechargerecord.adapter.RecordAdapter
import com.jsgt.greekfire.ui.mine.coindetail.recorddetail.RecordDetailActivity
import com.jsgt.greekfire.viewmodel.UserViewModel
import com.jsgt.greekfire.common.widget.LoadPageHolder
import com.jsgt.greekfire.event.USDTPriceEvent
import kotlinx.android.synthetic.main.recyclerview.*
import kotlinx.android.synthetic.main.recyclerview.recyclerview
import kotlinx.android.synthetic.main.recyclerview.refresh_layout

/**
 * 充币/提币记录
 * author fhx
 * create time 2020/4/24 14:15
 **/
class RechargeRecordActivity : BaseActivity() {
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(
            this,
            UserViewModelFactory()
        ).get(UserViewModel::class.java)
    }
    private var currentPage = 0
    private var isRefresh = true
    private var noMore = false//没有更多
    override fun getLayoutId(): Int = R.layout.recyclerview
    private val recordAdapter by lazy { RecordAdapter() }
    private lateinit var loadPageHolder: LoadPageHolder
    private var assetTypeId = 2

    companion object {
        private const val TYPE = "type"
        const val RECHARGE = 0
        const val WITHDRAW = 1

        private const val ASSET_TYPE = "assetType"
        const val GFC = 1
        const val USDT = 2
        const val GFT = 3

        fun start(context: Context, type: Int) {
            val intent = Intent(context, RechargeRecordActivity::class.java)
            intent.putExtra(TYPE, type)
            context.startActivity(intent)
        }

        /**
         * assetTypeId 提币记录类别
         */
        fun start(context: Context, type: Int, assetTypeId: Int) {
            val intent = Intent(context, RechargeRecordActivity::class.java)
            intent.putExtra(TYPE, type)
            intent.putExtra(ASSET_TYPE, assetTypeId)
            context.startActivity(intent)
        }
    }

    override fun main() {
        showTitleView()
        var type = intent.extras?.getInt(TYPE)
        assetTypeId = intent.extras?.getInt(ASSET_TYPE, 2)!!

        recordAdapter.TYPE = type!!

        refresh_layout.setOnRefreshListener {
            isRefresh = true
            noMore = false
            loadData(type, currentPage + 1)
        }

        loadData(type, 1)
       /* recordAdapter.itemClickListner = {
            RecordDetailActivity.start(
                this,
                type,
                it
            )
        }*/

        recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        recyclerview.adapter = recordAdapter

        userViewModel.coinBillLiveData.observe(this, Observer {
            loadFinish(it)
            refresh_layout.isRefreshing = false

        })
        userViewModel.withdrawLiveData.observe(this, Observer {
            loadFinish(it)
            refresh_layout.isRefreshing = false
        })
        userViewModel.errorLiveData.observe(this, Observer {
            emptyProcess()
            refresh_layout.isRefreshing = false
        })
        loadPageHolder = LoadPageHolder(this, title_layout)
        loadPageHolder.setLoadState(LoadPageHolder.LoadState.LOADING)
        loadPageHolder.setBackgroundClickEnable(true)
    }

    private fun loadData(type: Int, page: Int) {
        when (type) {
            RECHARGE -> {
                title = getString(R.string.coin_recharge_record)
//                userViewModel.assetsLogList("2", "1", page, PAGE_SIZE)
                userViewModel.rechargeLogList("2", "1", page, PAGE_SIZE)
            }
            WITHDRAW -> {
                title = getString(R.string.coin_withdraw_record)
                userViewModel.withdrawRecord(page, PAGE_SIZE, assetTypeId)
            }
        }
    }

    private fun emptyProcess() {

        if (recordAdapter!!.dataList().isEmpty()) {
            loadPageHolder.setLoadState(LoadPageHolder.LoadState.EMPTY)
        } else {
            loadPageHolder.setLoadState(LoadPageHolder.LoadState.SUCCESS)
        }
    }

    private fun loadFinish(list: List<BillBean>?) {
        if (list == null || list.isEmpty()) {
            emptyProcess()
            noMore = true

            return
        }
        if (isRefresh) {

            currentPage = 1
            recordAdapter.refreshData(list!!)
        } else {

            currentPage++
            recordAdapter.addData(list!!)
        }

        if (list.size < PAGE_SIZE) {
            noMore = true

        }
        if (recordAdapter!!.dataList().isEmpty()) {
            loadPageHolder.setLoadState(LoadPageHolder.LoadState.EMPTY)
        } else {
            loadPageHolder.setLoadState(LoadPageHolder.LoadState.SUCCESS)
        }
    }
}